use sea_orm_migration::prelude::*;

use crate::idents::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(User::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(User::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(User::Username)
                            .string()
                            .not_null()
                            .unique_key(),
                    )
                    .col(ColumnDef::new(User::PasswordDigest).string().not_null())
                    .col(ColumnDef::new(User::Active).boolean().not_null())
                    .col(ColumnDef::new(User::CreatedAt).timestamp())
                    .index(
                        Index::create()
                            .name("IDX_user_on_username")
                            .col(User::Username)
                            .unique()
                            .if_not_exists(),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(Session::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Session::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Session::UserId).integer().not_null())
                    .col(
                        ColumnDef::new(Session::Pubkey)
                            .binary()
                            .not_null()
                            .unique_key(),
                    )
                    .col(ColumnDef::new(Session::ExpiresAt).timestamp())
                    .index(
                        Index::create()
                            .name("IDX_session_on_id")
                            .if_not_exists()
                            .col(Session::Id)
                            .unique(),
                    )
                    .index(
                        Index::create()
                            .name("IDX_session_on_pubkey")
                            .if_not_exists()
                            .col(Session::Pubkey)
                            .unique(),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_session_user_id_to_user")
                            .from(Session::Table, Session::UserId)
                            .to(User::Table, User::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Session::Table).to_owned())
            .await?;

        manager
            .drop_table(Table::drop().table(User::Table).to_owned())
            .await?;

        Ok(())
    }
}
